package com.maixy.studyonlineapi.service;

import com.maixy.studyonlineapi.entity.Activity;
import com.maixy.studyonlineapi.entity.Media;
import com.maixy.studyonlineapi.enums.MediaTypeEnum;
import com.maixy.studyonlineapi.es.dao.ActivityEsDao;
import com.maixy.studyonlineapi.es.entity.ActivityEs;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.utils.OssUtil;
import com.maixy.studyonlineapi.utils.RandomUtil;
import com.maixy.studyonlineapi.view.MediaView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName CommonService
 * @Description TODO
 * @Date 2020/11/28 12:34
 * @Author maixy
 */
@Service
public class CommonService {
    @Resource
    private OssUtil ossUtil;
    @Resource
    private MediaService mediaService;
    @Resource
    private ActivityService activityService;
    @Resource
    private ActivityEsDao activityEsDao;

    public MediaView uploadImage(MultipartFile file) throws IOException {
        if (!Objects.requireNonNull(file.getContentType()).contains(MediaTypeEnum.IMAGE.getDesc())) {
            throw new TipException("文件类型错误");
        }
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            throw new TipException("文件名错误");
        }
        String suffix= originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = "/image/".concat(RandomUtil.randomStr(24).concat(suffix));
        ossUtil.upload(file.getBytes(), key);
        return mediaService.saveMedia(MediaTypeEnum.IMAGE.getCode(), key);
    }

    public MediaView uploadVideo(MultipartFile file) throws IOException {
        if (!Objects.requireNonNull(file.getContentType()).contains(MediaTypeEnum.VIDEO.getDesc())) {
            throw new TipException("文件类型错误");
        }
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            throw new TipException("文件名错误");
        }
        String suffix= originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = "/video/".concat(RandomUtil.randomStr(24).concat(suffix));
        ossUtil.upload(file.getBytes(), key);
        return mediaService.saveMedia(MediaTypeEnum.VIDEO.getCode(), key);
    }

    public MediaView uploadPDF(MultipartFile file) throws IOException {
        if (!Objects.requireNonNull(file.getContentType()).contains(MediaTypeEnum.PDF.getDesc())) {
            throw new TipException("文件类型错误");
        }
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isBlank(originalFilename)) {
            throw new TipException("文件名错误");
        }
        String suffix= originalFilename.substring(originalFilename.lastIndexOf("."));
        String key = "/pdf/".concat(RandomUtil.randomStr(24).concat(suffix));
        ossUtil.upload(file.getBytes(), key);
        return mediaService.saveMedia(MediaTypeEnum.PDF.getCode(), key);
    }

    public void getZip() {
        List<Media> mediaList = mediaService.findMediaByType(1);
        List<String> keyList = mediaList.stream().map(Media::getMediaUrl).collect(Collectors.toList());
        ossUtil.getOssZip("yasuo/".concat(String.valueOf(System.currentTimeMillis())).concat(".zip"), keyList);
    }

    public void buildActivityEsData() {
        List<Activity> activityList = activityService.findActivityList();
        for (Activity activity : activityList) {
            ActivityEs activityEs = new ActivityEs();
            BeanUtils.copyProperties(activity, activityEs);
            activityEsDao.save(activityEs);
        }
    }



}
